package org.exam.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.exam.blog.domain.Param;

import org.exam.blog.mapper.ParamMapper;
import org.exam.blog.query.MessageQuery;
import org.exam.blog.service.IParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ParamServiceImpl implements IParamService {
    @Autowired
    private ParamMapper paramMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    @Transactional
    public void add(Param param) {
        paramMapper.add(param);
        //添加
        mongoTemplate.save(param);

    }

    @Override
    @Transactional
    public void delete(Long id) {
        paramMapper.delete(id);
        //条件删除
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, Param.class);
    }

    @Override
    public Param get(Long id) {
        return paramMapper.get(id);
    }

    @Override
    public List<Param> getAll() {
        return paramMapper.getAll();
    }

    @Override
    @Transactional
    public void update(Param param) {
        paramMapper.update(param);
        //MongoDB修改
    }

    @Override
    public void patchDelete(Long[] ids) {
        paramMapper.patchDelete(ids);
    }

    @Override
    public PageInfo<Param> queryPage(MessageQuery messageQuery) {
        PageHelper.startPage(messageQuery.getCurrentPage(), messageQuery.getPageSize());
        List<Param> data = paramMapper.queryPage(messageQuery);
        return new PageInfo<>(data);
    }

    @Override
    public Map<String, Object> get() {
        List<Param> all = paramMapper.getAll();
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        for (Param param : all) {
            objectObjectHashMap.put(param.getParamKey(), param.getParamValue());
        }
        return objectObjectHashMap;
    }


}

